package com.chanjiedata.changbs.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;


import com.aigestudio.wheelpicker.WheelPicker;
import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.dialog.bean.FactoryBean;
import com.chanjiedata.changbs.proto.Mystock;
import com.chanjiedata.changbs.utlis.GsonUtils;
import com.chanjiedata.changbs.utlis.ValidateUtils;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import kale.ui.view.dialog.BaseCustomDialog;
import kale.ui.view.dialog.BaseEasyDialog;
import kale.ui.view.dialog.EasyDialog;

/**
 * Created by AndroidIntexh1 on 2018/9/25.
 */

public class WheelDialog extends BaseCustomDialog {

    public static final String KEY_DATA = "KEY_DATA";
    public static final String KEY_DATA_TYPE = "KEY_DATA_TYPE";

    public WheelDialog.OnDialogClickListener dialogListener;

    private String produce_name;
    private String produce_id;


    /**
     * 继承自{@link EasyDialog.Builder}以扩展builder
     */
    public static class Builder extends BaseEasyDialog.Builder<Builder> {

        private WheelDialog.OnDialogClickListener listener;
        private Bundle bundle = new Bundle();

        public Builder(@NonNull Context context) {
            super(context);
        }


        public Builder setData(String data) {
            bundle.putString(KEY_DATA, data);
            return this;
        }


        public Builder setDataType(String type) {
            bundle.putString(KEY_DATA_TYPE, type);
            return this;
        }

        public WheelDialog.Builder setOnDialogClickListener(WheelDialog.OnDialogClickListener lis) {
            listener = lis;
            return this;
        }

        @NonNull
        @Override
        protected EasyDialog createDialog() {
            WheelDialog dialog = new WheelDialog();
            dialog.setArguments(bundle); // 增加自己的bundle
            dialog.dialogListener = listener;
            return dialog;
        }
    }


    @Override
    protected int getLayoutResId() {
        return R.layout.dialog_wheel_produce;
    }

    @Override
    protected void bindViews(View view) {
        WheelPicker wheelPicker = findView(R.id.wheel_produce);
        TextView btn_cancle = findView(R.id.tv_cancel);
        TextView btn_sure = findView(R.id.tv_sure);
        wheelPicker.setCurtain(true);
        wheelPicker.setItemSpace(10);
        wheelPicker.setCurtainColor(Color.parseColor("#50d7d7d7"));
        String type = getArguments().getString(KEY_DATA_TYPE,"0"); // 0 默认为调拨机器类型  1 ：自备机类型

        if (type.equals("0")) {
            List<Mystock.myMachine.List> wheel = GsonUtils.jsonToBeanList(getArguments().getString(KEY_DATA), new TypeToken<List<Mystock.myMachine.List>>() {
            }.getType());
            if (!ValidateUtils.isValidate(wheel))
            {
                dismiss();
                return;
            }
            List<String> wheelData = new ArrayList<>();
            produce_name = wheel.get(0).getGcName() + wheel.get(0).getGoodsName() + wheel.get(0).getGoodsSerial();
            produce_id = String.valueOf(wheel.get(0).getGoodsId());
            for (Mystock.myMachine.List dataBean : wheel) {
                wheelData.add(dataBean.getGcName() + dataBean.getGoodsName() + dataBean.getGoodsSerial());
            }
            wheelPicker.setData(wheelData);
            btn_cancle.setOnClickListener(view1 -> dismiss());
            btn_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialogListener.onClick(view,produce_name,produce_id);
                    dismiss();
                }
            });
            wheelPicker.setOnItemSelectedListener((picker, data, position) -> {
                Log.e("mango", "bindViews: " + data.toString() + position);
                produce_name = wheel.get(position).getGcName() + wheel.get(position).getGoodsName() + wheel.get(position).getGoodsSerial();
                produce_id = String.valueOf(wheel.get(position).getGoodsId());
            });
        }else if (type.equals("1"))
        {
            List<FactoryBean> wheel = GsonUtils.jsonToBeanList(getArguments().getString(KEY_DATA), new TypeToken<List<FactoryBean>>() {
            }.getType());
            if (!ValidateUtils.isValidate(wheel))
            {
                dismiss();
                return;
            }
            List<String> wheelData = new ArrayList<>();
            produce_name = wheel.get(0).getName();
            produce_id = String.valueOf(wheel.get(0).getId());
            for (FactoryBean dataBean : wheel) {
                wheelData.add(dataBean.getName());
            }
            wheelPicker.setData(wheelData);
            btn_cancle.setOnClickListener(view1 -> dismiss());
            btn_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialogListener.onClick(view,produce_name,produce_id);
                    dismiss();
                }
            });
            wheelPicker.setOnItemSelectedListener((picker, data, position) -> {
                Log.e("mango", "bindViews: " + data.toString() + position);
                produce_name = wheel.get(position).getName();
                produce_id = String.valueOf(wheel.get(position).getId());
            });
        }


    }

    @Override
    protected void setViews() {
        setBackground();
        setLayout();

    }

    /**
     * 强制取消背景，保持有透明
     */
    private void setBackground() {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable()); // 去除dialog的背景，即透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(0xffffffff)); // 设置白色背景
//        getDialog().getWindow().setBackgroundDrawableResource(R.drawable.dialog_shop_jion_bg); // 设置背景
    }


    /**
     * 也可通过setLayout来设置：
     * getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
     */
    private void setLayout() {
//        Window window = getDialog().getWindow();
//        final WindowManager.LayoutParams lp = window.getAttributes();
//
//        // 强制宽高
//        int padding = getResources().getDimensionPixelOffset(R.dimen.dialog_padding);
//        lp.width = getScreenWidth(getActivity()) - (padding * 2);
////        lp.height = getResources().getDimensionPixelOffset(R.dimen.dialog_height);
//
//        lp.gravity = Gravity.TOP; // 设置展示的位置
//        lp.y = getResources().getDimensionPixelOffset(R.dimen.join_shop_dialog_margin);
//        window.setAttributes(lp);
    }

    @Override
    protected void modifyAlertDialogBuilder(AlertDialog.Builder builder) {
        super.modifyAlertDialogBuilder(builder);
    }

    public static int getScreenWidth(Activity activity) {
        final DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        return dm.widthPixels;
    }


    /**
     * 监听输入框内容
     */
    public interface OnDialogClickListener {
        /**
         * Called when a view has been clicked.
         *
         * @param v The view that was clicked.
         */
        void onClick(View v, String goods_id, String goods_name);
    }

}
